package com.tongfusoft.document.manager.folders.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.dto.TreeDto;
import com.tongfusoft.document.entity.Folders;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.folders.service.IFoldersService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("foldersService")
public class FoldersServiceImpl extends DataTemplateServiceImpl implements
		IFoldersService {

	@Autowired
	@Qualifier(value = "foldersService")
	public IFoldersService iFoldersService;

	//获得树文件夹
	@Override
	public TreeDto foldersList(Folders folders) {

		Folders folder = (Folders) this.selectList("foldersList", folders)
				.get(0);

		TreeDto treeDto = new TreeDto();
		treeDto.setText(folder.getFolderName());
		treeDto.setHref("");
		String[] tags = new String[1];
		tags[0] = StringUtils.isEmpty(folder.getFolderId()) ? "" : folder
				.getFolderId();
		treeDto.setTags(tags);
		treeDto.setNodes(this.getChildFoldersList(folder.getFolderId()));

		return treeDto;
	}

	public List<TreeDto> getChildFoldersList(String folderId) {

		Folders child = new Folders();
		child.setParentFolderId(folderId);
		List<Folders> folderChildList = this.selectList("getChildFoldersList",
				child);
		List<TreeDto> treeList = new ArrayList();
		for (Folders folder : folderChildList) {
			TreeDto treeDto = new TreeDto();

			treeDto.setText(folder.getFolderName());
			treeDto.setHref("");
			String[] tags = new String[1];
			tags[0] = folder.getFolderId();
			treeDto.setTags(tags);
			treeDto.setNodes(getChildFoldersList(folder.getFolderId()));
			treeList.add(treeDto);
		}

		return treeList;
	}

	@Override
	public void updateFolders(Folders folders, Users user) {
		
		Folders detailFolders = this.iFoldersService.getFolders(folders);
		detailFolders.setFolderName(folders.getFolderName());
		
		if (this.selectList("getFolderByNameOutSelf", detailFolders).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		this.update("updateFolders", detailFolders);

	}

	@Override
	public void createFolders(Folders folders, Users user) {
		
		Folders folders2 = new Folders();
		folders2.setParentFolderId(folders.getFolderId());
		folders2.setFolderName(folders.getFolderName());

		if (this.selectList("getFolderByName", folders2).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		folders.setParentFolderId(folders.getFolderId());
		folders.setFolderId(UIDGeneratorService.getUUID());
		folders.setStatus("1");
		folders.setCreatedBy(user.getUserId());
		folders.setCreatedDate(new Timestamp(new Date().getTime()));
		folders.setLastUpdBy(user.getUserId());
		folders.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.insert("createFolders", folders);

	}
	
	@Override
	public void deleteFolders(Folders folders) {

		this.delete("deleteFolders", folders);

	}

	@Override
	public Folders getFolders(Folders folders) {
		
		return (Folders) this.selectOne("getFolders", folders);
	}

}
